<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * FormativeLab
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		FormativeLab
 * @author		FormativeLab Dev Team
 * @copyright	Copyright (c) 2011 - 2013, FormativeLab, Inc.
 * @license		http://www.formativesoft.com/license/
 * @link		http://www.formativesoft.com/
 * @since		Version 2.0
 * @filesource ./application/helpers/dom_helper.php
 */

// ------------------------------------------------------------------------

/**
 * FormativeLab DOM Helpers
 *
 * @package		FormativeLab
 * @subpackage	Helpers
 * @category	Helpers
 * @author		FormativeLab Dev Team
 * @link		http://www.formativesoft.com/
 */

 // ------------------------------------------------------------------------

/**
 * createDocument() - Contains five input parameters and all are optional 
 * Its return an DOMDocument for generating xml response
 *
 * @access	public
 * @param	rootElement
 * @param	dtdFileName
 * @param	xmlVersion
 * @param	encoding
 * @param	standalone
 * @return  DOMDocument
 */
if (!function_exists('createDocument')){
	function createDOMDocument($rootElement = 'root', $dtdFileName = NULL, $xmlVersion = '1.0', $encoding = 'UTF-8', $standalone  = true){
		$dtdFileName		= $dtdFileName == NULL ? "{$rootElement}.dtd" : $dtdFileName;		
		$imp				= new DOMImplementation;
		$dtd				= $imp->createDocumentType($rootElement, '', $dtdFileName);
		$dom				= $imp->createDocument("", "", $dtd);		
		$dom->xmlVersion 	= $xmlVersion;
		$dom->encoding		= $encoding;
		$dom->standalone	= $standalone;
		
        return $dom;
	}
}


 // ------------------------------------------------------------------------

/**
 * createAttribute() - Contains four required input parameters  
 * Its return an DOMElement for generating xml
 *
 * @access	public
 * @param	dom
 * @param	node
 * @param	attrName
 * @param	attrValue
 * @return  DOMElement
 */
if (!function_exists('createAttribute')){
	function createAttribute(DOMDocument &$dom,DOMElement &$node, $attrName, $attrValue){
		$node->appendChild($dom->createAttribute($attrName))
			 ->appendChild($dom->createTextNode($attrValue));
		
        return $node;
	}
}

 // ------------------------------------------------------------------------

/**
 * createRootElement() - Contains two input parameters. First on is  mandatory
 * second is optional the name of root element 
 *
 * @access	public
 * @param	dom
 * @param	elementName
 * @return  DOMElement
 */
if (!function_exists('createRootElement')){
	function createRootElement(DOMDocument &$dom, $rootElement = 'root'){
		$root = $dom->appendChild($dom->createElement($rootElement));
        return $root;
	}
}

 // ------------------------------------------------------------------------

/**
 * appendChild() - Contains tree required input parameters.
 *
 * @access	public
 * @param	dom
 * @param	root
 * @param	name
 * @param	value
 * @return  DOMElement
 */
if (!function_exists('createElement')){
	function appendChild(DOMDocument &$dom, DOMElement &$root, $name, $value = null){
		$node  = $root->appendChild($dom->createElement($name));
		if(isset($value) && !(is_array($value) || is_object($value))){
			$node->appendChild($dom->createTextNode($value));
		}
        return $node;
	}
}

 // ------------------------------------------------------------------------

/**
 * createSimpleXMLElement() - Contains an input parameters. Which one can
 * hold XML String or XML File Path
 *
 * @access	public
 * @param	xml
 * @return  SimpleXMLElement
 */
if (!function_exists('createSimpleXMLElement')){
	function createSimpleXMLElement($xml, $data){
		$simpleXMLElement = new SimpleXMLElement($xml);
		if(isset($data)){
			$data = filterRecords($data);
			foreach($data as $key => &$value){
				$key = resolveName($key, $value);
				encodeSimpleXMLElement($simpleXMLElement, $key, $value);
			}
		}
        return $simpleXMLElement;
	}
}

// ------------------------------------------------------------------------

/**
 * encodeSimpleXMLElement() - Contains four input parameters, all are mandatory 
 *
 * @access	public
 * @param	root
 * @param	elementName
 * @param	value
 * @return  SimpleXMLElement
 */
if (!function_exists('encodeSimpleXMLElement')){
	function encodeSimpleXMLElement(SimpleXMLElement &$root, $elementName, $object){
		if(isset($object)){
			if(!is_array($object) && !is_object($object)){
				$root->addChild($elementName, $object);
			}else{
				$node  = $root->addChild($elementName);
				foreach($object as $attrName => &$attrValue){
					$attrName	= resolveName($attrName, $attrValue);
					if(!is_array($attrValue) && !is_object($attrValue)){
						$attrValue	= filterBoolean($attrValue);
						$node->addAttribute($attrName, $attrValue);
					}else{
						encodeSimpleXMLElement($node, $attrName, $attrValue);						
					}
				}
			}
		}		
        return $root;
	}
}

// ------------------------------------------------------------------------

/**
 * convertAsDOMDocument() with a single required parameter
 *
 * @access	public
 * @param   simpleXMLElement
 * @param	xmlVersion
 * @param	encoding
 * @param	standalone
 * @return	DOMDocument
 */
if (!function_exists('convertAsDOMDocument')){
	function convertAsDOMDocument(SimpleXMLElement $simpleXMLElement, $xmlVersion = '1.0', $encoding = 'UTF-8', $standalone  = true){	
		$dom  				= new DOMDocument();	
		$dom->loadXML($simpleXMLElement->asXML());
		$dom->xmlVersion 	= $xmlVersion;
		$dom->encoding		= $encoding;
		$dom->standalone	= $standalone;
		return $dom;
	}
}

// ------------------------------------------------------------------------

/**
 * encodeDOMElement() - Contains four input parameters, all are mandatory 
 *
 * @access	public
 * @param	dom
 * @param	root
 * @param	elementName
 * @param	object
 * @return  DOMDocument
 */
if (!function_exists('encodeDOMElement')){
	function encodeDOMElement(DOMDocument &$dom, DOMElement &$root, $elementName, $object){
		if(isset($object)){
			if(is_array($object) || is_object($object)){
				$node	= appendChild($dom, $root, $elementName);
				foreach($object as $attrName => &$attrValue){
					$attrName	= resolveName($attrName, $attrValue);
					if(!is_array($attrValue) && !is_object($attrValue)){
						$attrValue	= filterBoolean($attrValue);
						createAttribute($dom, $node, $attrName, $attrValue);
					}else{
						encodeDOMElement($dom, $node, $attrName, $attrValue);						
					}
				}
			}
		}		
        return $dom;
	}
}

// ------------------------------------------------------------------------

/**
 * filterBoolean() - Contains an input parameters
 *
 * @access	public
 * @param	value
 * @return  bool
 */
if (!function_exists('filterBoolean')){
	function filterBoolean($value){
		if(isset($value) && is_bool($value)){
			if($value){
				return 'true';
			}else {
				return 'false';
			}
		}		
        return $value;
	}
}

/**
 * resolveName() - Contains an input parameters
 *
 * @access	public
 * @param	name
 * @param	value
 * @return  string
 */
if (!function_exists('resolveName')){
	function resolveName(&$name, $value){
		$pattern	= '/^[a-zA-Z]{1}[\\w]*?/';
		if(isset($name) && isset($value)){
			if(preg_match($pattern, $name)){
				return $name;
			}else if(is_object($value)){
				$name	= get_class($value);
				return $name;
			}
		}
		return $name;
	}
}

// ------------------------------------------------------------------------

/**
 * renderDOMAsXML() use for xml response
 *
 * @access	public
 * @param   dom
 * @param   format
 * @param   status
 * @return	void
 */
if (!function_exists('renderDOMAsXML')){
	function renderDOMAsXML(DOMDocument &$dom, $format = true, $status = 200){
		$dom->formatOutput 	= $format;
		$controller 		= &get_instance();
		
		$controller->output->set_status_header($status);
		$controller->output->set_header('Content-type: application/xml; charset=utf-8');
		$controller->output->set_output($dom->saveXML());
	}
}

// ------------------------------------------------------------------------

/**
 * renderDataAsXML() use for xml response
 *
 * @access	public
 * @param   dom
 * @param   rootElement
 * @param   format
 * @param   status
 * @return	void
 */
if (!function_exists('renderDataAsXML')){
	function renderDataAsXML($data, $rootElement = 'root', $format = true, $status = 200){	
		$data = filterRecords($data);
		$dom  = createDOMDocument($rootElement);
		$root = createRootElement($dom, $rootElement);
		
		foreach($data as $key => &$value){
			$key = resolveName($key, $value);
			encodeDOMElement($dom, $root, $key, $value);
		}
		renderDOMAsXML($dom, $format, $status);
	}
}

// ------------------------------------------------------------------------

/**
 * renderSimpleXMLElementAsXML() use for xml response
 *
 * @access	public
 * @param   simpleXMLElement
 * @param   format
 * @param   status
 * @return	void
 */
if (!function_exists('renderSimpleXMLElementAsXML')){
	function renderSimpleXMLElementAsXML($data, $format = true, $status = 200){
		$xml				= '<root/>';
		$simpleXMLElement	= createSimpleXMLElement($xml, $data);
		$dom				= convertAsDOMDocument($simpleXMLElement);
		renderDOMAsXML($dom, $format, $status);
	}
}

/* End of file dom_helper.php */
/* Location: ./application/helpers/dom_helper.php */